home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2001 May / macformat_103_may_2001.iso / Mac OS X Shareware / Fizilla / Components / chatzilla-service.js < prev    next >
Encoding:
Text File  |  2001-03-26  |  10.1 KB  |  334 lines  |  [TEXT/CWIE]

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is mozilla.org code.
  13.  * 
  14.  * The Initial Developer of the Original Code is Netscape
  15.  * Communications Corporation.  Portions created by Netscape are
  16.  * Copyright (C) 1999 Netscape Communications Corporation.  All
  17.  * Rights Reserved.
  18.  * 
  19.  * Contributor(s): 
  20.  * Seth Spitzer <sspitzer@netscape.com>
  21.  * Robert Ginda <rginda@netscape.com>
  22.  */
  23.  
  24. /*
  25.  * This file contains the following chatzilla related components:
  26.  * 1. Command line handler service, for responding to the -chat command line
  27.  *    option. (CLineHandler)
  28.  * 2. Content handler for responding to content of type x-application-irc
  29.  *    (IRCContentHandler)
  30.  * 3. Protocol handler for supplying a channel to the browser when an irc://
  31.  *    link is clicked. (IRCProtocolHandler)
  32.  * 4. A (nearly empty) imeplementation of nsIChannel for telling the browser
  33.  *    that irc:// links have the content type x-application-irc (BogusChannel)
  34.  */
  35.  
  36. /* components defined in this file */
  37. const CLINE_SERVICE_CONTRACTID =
  38.     "@mozilla.org/commandlinehandler/general-startup;1?type=chat";
  39. const CLINE_SERVICE_CID =
  40.     Components.ID("{38a95514-1dd2-11b2-97e7-9da958640f2c}");
  41. const IRCCNT_HANDLER_CONTRACTID =
  42.     "@mozilla.org/uriloader/content-handler;1?type=x-application-irc";
  43. const IRCCNT_HANDLER_CID =
  44.     Components.ID("{98919a14-1dd1-11b2-be1a-b84344307f0a}");
  45. const IRCPROT_HANDLER_CONTRACTID =
  46.     "@mozilla.org/network/protocol;1?name=irc";
  47. const IRCPROT_HANDLER_CID =
  48.     Components.ID("{f21c35f4-1dd1-11b2-a503-9bf8a539ea39}");
  49.  
  50. /* components used in this file */
  51. const MEDIATOR_CONTRACTID =
  52.     "@mozilla.org/rdf/datasource;1?name=window-mediator"
  53. const SIMPLEURI_CONTRACTID = 
  54.     "@mozilla.org/network/simple-uri;1";
  55. const ASS_CONTRACTID =
  56.     "@mozilla.org/appshell/appShellService;1";
  57.  
  58. /* interafces used in this file */
  59. const nsIWindowMediator  = Components.interfaces.nsIWindowMediator;
  60. const nsICmdLineHandler  = Components.interfaces.nsICmdLineHandler;
  61. const nsICategoryManager = Components.interfaces.nsICategoryManager;
  62. const nsIContentHandler  = Components.interfaces.nsIContentHandler;
  63. const nsIProtocolHandler = Components.interfaces.nsIProtocolHandler;
  64. const nsIURI             = Components.interfaces.nsIURI;
  65. const nsIChannel         = Components.interfaces.nsIChannel;
  66. const nsIRequest         = Components.interfaces.nsIRequest;
  67. const nsIAppShellService = Components.interfaces.nsIAppShellService;
  68. const nsISupports        = Components.interfaces.nsISupports;
  69.  
  70. /* Command Line handler service */
  71. function CLineService()
  72. {}
  73.  
  74. CLineService.prototype.commandLineArgument = "-chat";
  75. CLineService.prototype.prefNameForStartup = "general.startup.chat";
  76. CLineService.prototype.chromeUrlForTask="chrome://chatzilla/content";
  77. CLineService.prototype.helpText = "Start with an IRC chat client";
  78. CLineService.prototype.handlesArgs=false;
  79. CLineService.prototype.defaultArgs ="";
  80. CLineService.prototype.openWindowWithArgs=false;
  81.  
  82. /* factory for command line handler service (CLineService) */
  83. var CLineFactory = new Object();
  84.  
  85. CLineFactory.createInstance =
  86. function (outer, iid) {
  87.     if (outer != null)
  88.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  89.  
  90.     if (!iid.equals(nsICmdLineHandler) && !iid.equals(nsISupports))
  91.         throw Components.results.NS_ERROR_INVALID_ARG;
  92.  
  93.     return new CLineService();
  94. }
  95.  
  96. /* x-application-irc content handler */
  97. function IRCContentHandler ()
  98. {}
  99.  
  100. IRCContentHandler.prototype.QueryInterface =
  101. function (iid) {
  102.  
  103.     if (!iid.equals(nsIContentHandler))
  104.         throw Components.results.NS_ERROR_NO_INTERFACE;
  105.  
  106.     return this;
  107. }
  108.  
  109. IRCContentHandler.prototype.handleContent =
  110. function (aContentType, aCommand, aWindowTarget, aSourceContext, aRequest)
  111. {
  112.     var e;
  113.     var channel = aRequest.QueryInterface(nsIChannel);
  114.  
  115.     dump ("ircLoader.handleContent (" + aContentType + ", " +
  116.           aCommand + ", " + aWindowTarget + ", " + aSourceContext + ", " +
  117.           channel.URI.spec + ")\n");
  118.     
  119.     var windowManager =
  120.         Components.classes[MEDIATOR_CONTRACTID].getService(nsIWindowMediator);
  121.  
  122.     var w = windowManager.getMostRecentWindow("irc:chatzilla");
  123.  
  124.     if (w)
  125.     {
  126.         w.focus();
  127.         w.gotoIRCURL(channel.URI.spec);
  128.     }
  129.     else
  130.     {
  131.         var ass =
  132.             Components.classes[ASS_CONTRACTID].getService(nsIAppShellService);
  133.         w = ass.getHiddenDOMWindow();
  134.         w.openDialog("chrome://chatzilla/content/chatzilla.xul?" + 
  135.                      channel.URI.spec, "_blank",
  136.                      "chrome,menubar,toolbar,resizable");
  137.     }
  138.     
  139. }
  140.  
  141. /* content handler factory object (IRCContentHandler) */
  142. var IRCContentHandlerFactory = new Object();
  143.  
  144. IRCContentHandlerFactory.createInstance =
  145. function (outer, iid) {
  146.     if (outer != null)
  147.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  148.  
  149.     if (!iid.equals(nsIContentHandler) && !iid.equals(nsISupports))
  150.         throw Components.results.NS_ERROR_INVALID_ARG;
  151.  
  152.     return new IRCContentHandler();
  153. }
  154.  
  155. /* irc protocol handler component */
  156. function IRCProtocolHandler()
  157. {
  158. }
  159.  
  160. IRCProtocolHandler.prototype.scheme = "irc";
  161. IRCProtocolHandler.prototype.defaultPort = 6667;
  162.  
  163. IRCProtocolHandler.prototype.newURI =
  164. function (aSpec, aBaseURI)
  165. {
  166.     if (aBaseURI)
  167.     {
  168.         dump ("-*- ircHandler: aBaseURI passed to newURI, bailing.\n");
  169.         return null;
  170.     }
  171.     
  172.     var uri = Components.classes[SIMPLEURI_CONTRACTID].createInstance(nsIURI);
  173.     uri.spec = aSpec;
  174.     
  175.     return uri;
  176. }
  177.  
  178. IRCProtocolHandler.prototype.newChannel =
  179. function (aURI)
  180. {
  181.     return new BogusChannel (aURI);
  182. }
  183.  
  184. /* protocol handler factory object (IRCProtocolHandler) */
  185. var IRCProtocolHandlerFactory = new Object();
  186.  
  187. IRCProtocolHandlerFactory.createInstance =
  188. function (outer, iid) {
  189.     if (outer != null)
  190.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  191.  
  192.     if (!iid.equals(nsIProtocolHandler) && !iid.equals(nsISupports))
  193.         throw Components.results.NS_ERROR_INVALID_ARG;
  194.  
  195.     return new IRCProtocolHandler();
  196. }
  197.  
  198. /* bogus IRC channel used by the IRCProtocolHandler */
  199. function BogusChannel (aURI)
  200. {
  201.     this.URI = aURI;
  202.     this.originalURI = aURI;
  203. }
  204.  
  205. BogusChannel.prototype.QueryInterface =
  206. function (iid) {
  207.  
  208.     if (!iid.equals(nsIChannel) && !iid.equals(nsIRequest) &&
  209.         !iid.equals(nsISupports))
  210.         throw Components.results.NS_ERROR_NO_INTERFACE;
  211.  
  212.     return this;
  213. }
  214.  
  215. /* nsIChannel */
  216. BogusChannel.prototype.loadAttributes = null;
  217. BogusChannel.prototype.contentType = "x-application-irc";
  218. BogusChannel.prototype.contentLength = 0;
  219. BogusChannel.prototype.owner = null;
  220. BogusChannel.prototype.loadGroup = null;
  221. BogusChannel.prototype.notificationCallbacks = null;
  222. BogusChannel.prototype.securityInfo = null;
  223.  
  224. BogusChannel.prototype.open =
  225. BogusChannel.prototype.asyncOpen =
  226. function ()
  227. {
  228.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  229. }
  230.  
  231. BogusChannel.prototype.asyncOpen =
  232. function (observer, ctxt)
  233. {
  234.     observer.onStartRequest (this, ctxt);
  235. }
  236.  
  237. BogusChannel.prototype.asyncRead =
  238. function (listener, ctxt)
  239. {
  240.     return listener.onStartRequest (this, ctxt);
  241. }
  242.  
  243. /* nsIRequest */
  244. BogusChannel.prototype.isPending =
  245. function ()
  246. {
  247.     return true;
  248. }
  249.  
  250. BogusChannel.prototype.status = Components.results.NS_OK;
  251.  
  252. BogusChannel.prototype.cancel =
  253. function (aStatus)
  254. {
  255.     this.status = aStatus;
  256. }
  257.  
  258. BogusChannel.prototype.suspend =
  259. BogusChannel.prototype.resume =
  260. function ()
  261. {
  262.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  263. }
  264.  
  265. var ChatzillaModule = new Object();
  266.  
  267. ChatzillaModule.registerSelf =
  268. function (compMgr, fileSpec, location, type)
  269. {
  270.     dump("*** Registering -chat handler.\n");
  271.     compMgr.registerComponentWithType(CLINE_SERVICE_CID,
  272.                                       "Chatzilla CommandLine Service",
  273.                                       CLINE_SERVICE_CONTRACTID, fileSpec,
  274.                                       location, true, true, type);
  275.     
  276.     catman = Components.classes["@mozilla.org/categorymanager;1"]
  277.         .getService(nsICategoryManager);
  278.     catman.addCategoryEntry("command-line-argument-handlers",
  279.                             "chatzilla command line handler",
  280.                             CLINE_SERVICE_CONTRACTID, true, true);
  281.  
  282.     dump("*** Registering x-application-irc handler.\n");
  283.     compMgr.registerComponentWithType(IRCCNT_HANDLER_CID,
  284.                                       "IRC Content Handler",
  285.                                       IRCCNT_HANDLER_CONTRACTID, fileSpec,
  286.                                       location, true, true, type);
  287.  
  288.     dump("*** Registering irc protocol handler.\n");
  289.     compMgr.registerComponentWithType(IRCPROT_HANDLER_CID,
  290.                                       "IRC protocol handler",
  291.                                       IRCPROT_HANDLER_CONTRACTID, fileSpec, location,
  292.                                       true, true, type);
  293.  
  294. }
  295.  
  296. ChatzillaModule.unregisterSelf =
  297. function(compMgr, fileSpec, location)
  298. {
  299.     compMgr.unregisterComponentSpec(CLINE_SERVICE_CID, fileSpec);
  300.     catman = Components.classes["@mozilla.org/categorymanager;1"]
  301.         .getService(nsICategoryManager);
  302.     catman.deleteCategoryEntry("command-line-argument-handlers",
  303.                                CLINE_SERVICE_CONTRACTID, true);
  304. }
  305.  
  306. ChatzillaModule.getClassObject =
  307. function (compMgr, cid, iid) {
  308.     if (cid.equals(CLINE_SERVICE_CID))
  309.         return CLineFactory;
  310.  
  311.     if (cid.equals(IRCCNT_HANDLER_CID))
  312.         return IRCContentHandlerFactory;
  313.  
  314.     if (cid.equals(IRCPROT_HANDLER_CID))
  315.         return IRCProtocolHandlerFactory;
  316.     
  317.     if (!iid.equals(Components.interfaces.nsIFactory))
  318.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  319.  
  320.     throw Components.results.NS_ERROR_NO_INTERFACE;
  321.     
  322. }
  323.  
  324. ChatzillaModule.canUnload =
  325. function(compMgr)
  326. {
  327.     return true;
  328. }
  329.  
  330. /* entrypoint */
  331. function NSGetModule(compMgr, fileSpec) {
  332.     return ChatzillaModule;
  333. }
  334.